﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleGameData.GameObjects.Particles;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SimpleGame.Utils;
using System.Diagnostics;

namespace SimpleGame.GameObjects.Particles
{
    public class Emitter
    {
        #region Fields

        // The emitter data read from a xml file
        private EmitterData emitterData;
        // List of particles this emitter "has"
        private List<Particle> particles;

        #endregion

        #region Properties

        /// <summary>
        /// The emitter data read from a xml file
        /// </summary>
        public EmitterData EmitterData
        {
            get { return emitterData; }
            set { emitterData = value; }
        }

        /// <summary>
        /// List of particles this emitter has available
        /// </summary>
        public List<Particle> Particles
        {
            get { return particles; }
            set { particles = value; }
        }

        #endregion

        #region Constructor

        public Emitter()
        {
            particles = new List<Particle>();
        }

        #endregion

        #region General Methods

        /// <summary>
        /// Adds an amount of "empty" particles to the emitter
        /// </summary>
        /// <param name="x">The amount of particles to add</param>
        public void AddParticles(int x)
        {
            for (int i = 0; i < x; i++)
            {
                particles.Add(new Particle());
            }
        }

        #endregion

        #region Update Method

        public void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            foreach (Particle particle in particles)
            {
                // if particle is not alive anymore reuse the particle by randomizing its fields and setting it to alive again
                if (!particle.IsAlive)
                {
                    particle.ParticleData.Texture = emitterData.Texture;
                    particle.ParticleData.Position = emitterData.Position;
                    particle.Life = RandomGenerator.Float(emitterData.MinLife, emitterData.MaxLife);
                    particle.ParticleData.Velocity = RandomGenerator.Float(emitterData.MinVelocity, emitterData.MaxVelocity);
                    particle.ParticleData.Direction = new Vector2(RandomGenerator.Float(0.0f, 1.0f), RandomGenerator.Float(0.0f, 1.0f));
                    particle.ParticleData.Rotation = RandomGenerator.Float(0, 360f);
                    float scale = RandomGenerator.Float(0.75f, 1.25f);
                    particle.ParticleData.Scale = new Vector2(scale, scale);
                    particle.ParticleData.TintColor = new Color(
                        RandomGenerator.Float(0f, 1f),
                        RandomGenerator.Float(0f, 1f),
                        RandomGenerator.Float(0f, 1f),
                        RandomGenerator.Float(0f, 1f));

                    particle.IsAlive = true;
                }

                particle.Update(gameTime);
            }
        }

        #endregion

        #region Draw Method

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Particle particle in particles)
            {
                particle.Draw(spriteBatch);
            }
        }

        #endregion
    }
}
